import os,sys


def parse_nan(files_list):
    print "Start parsing .dat files"
    for file_name in files_list:
        if file_name.startswith("out-") and file_name.endswith(".dat"):
            print "Processing file %s" % file_name
            file_in = open(file_name, 'r')
            ss = file_in.readlines()
            file_in.close()
            
            file_out = open(file_name, 'w')
            for s in ss:
                s_new = s.replace("NaN", "0.0")
                file_out.write(s_new)
            file_out.close()
            print "End file"
        else:
            pass
    
    print "End parsing"

def clean_dat(files_list):
    for file_name in files_list:
        fn = file_name.lower()
        if fn.startswith("out-") and fn.endswith(".dat"):
            print "Removing file %s" % file_name
            os.remove(file_name)
        else:
            pass
    
    print "End clean"
    
def clean_png(files_list):
    for file_name in files_list:
        fn = file_name.lower()
        if (fn.startswith("disl") or fn.startswith("out")) and fn.endswith(".png"):
            print "Removing file %s" % file_name
            os.remove(file_name)
        else:
            pass
    
    print "End clean"

if __name__ == "__main__":
    if len(sys.argv) > 1:
        files_list = os.listdir(".")
        if sys.argv[1].startswith("nan"):
            parse_nan(files_list)
        elif sys.argv[1].startswith('dat'):
            clean_dat(files_list)
        elif sys.argv[1].startswith('png'):
            clean_png(files_list)
    else:
        print "Usage: output_pasrser.py [command]"
        print "nan - fix nan"
        print "dat - remove dat"
        print "png - remove png"
